package com.geeklan.smart.ui.device;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.app.ActivityCompat;
import androidx.core.content.res.ResourcesCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.geeklan.base.base.BaseActivity;
import com.geeklan.base.utils.MLog;
import com.geeklan.smart.R;
import com.geeklan.smart.common.Constants;
import com.geeklan.smart.data.model.DeviceStatusResponse;
import com.geeklan.smart.data.model.FeedRule;
import com.geeklan.smart.data.model.FeedingRecord;
import com.geeklan.smart.data.remote.rtc.RtcEngineManager;
import com.geeklan.smart.data.repository.device.DeviceRepository;
import com.geeklan.smart.data.repository.pet.PetRepository;
import com.geeklan.smart.databinding.ActivityDeviceInfoBinding;
import com.geeklan.smart.event.LoginOut;
import com.geeklan.smart.permission.PermissionManager;
import com.geeklan.smart.ui.dialog.FoodBottomSheetDialog;
import com.geeklan.smart.ui.feed.FeedPlanActivity;
import com.geeklan.smart.ui.feed.FeedingRecordsActivity;
import com.geeklan.smart.ui.feed.fragment.adapter.FeedingRecordsAdapter;
import com.geeklan.smart.ui.viewmodel.device.DeviceViewModel;
import com.geeklan.smart.ui.viewmodel.device.DeviceViewModelFactory;
import com.geeklan.smart.ui.viewmodel.pet.PetViewModel;
import com.geeklan.smart.ui.viewmodel.pet.PetViewModelFactory;
import com.geeklan.smart.utils.DateUtils;
import com.geeklan.smart.utils.DensityUtils;
import com.geeklan.smart.utils.GlideUtils;
import com.geeklan.smart.utils.SharedPreferencesUtil;
import com.geeklan.smart.utils.TimeUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class DeviceInfoActivity extends BaseActivity<ActivityDeviceInfoBinding> {
    private static final int GET_DEVICE_STATUS = 3;
    private DeviceViewModel deviceViewModel;
    private PetViewModel petViewModel;
    private String token;
    private String token1;
    private int userId = 0;
    private RtcEngineManager rtcEngineManager;
    private RtcEngineManager rtcEngineManager1;
    // 填写频道名
    private String deviceId;
    private String deviceName;
    private String preview;
    private String preview1;
    private FoodBottomSheetDialog foodBottomSheetDialog;
    private int typePosition;
    private boolean isBackPlayVideo = false;
    private DeviceStatusResponse.DataDTO device;
    private FeedingRecordsAdapter mAdapter;
    private List<FeedingRecord> feedingRecords = new ArrayList<>();
    private List<DateUtils.DateAndWeek> datesAndWeeks;
    private String date;

    @Override
    public boolean isRegisterEvent() {
        return true;
    }

    @Override
    protected ActivityDeviceInfoBinding initBinding() {
        return ActivityDeviceInfoBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        loading();
        date = DateUtils.dateFormatDay1(System.currentTimeMillis());
        userId = SharedPreferencesUtil.getInt("userId", 0);
        deviceId = getIntent().getStringExtra("deviceId");
        deviceName = getIntent().getStringExtra("deviceName");
        preview = getIntent().getStringExtra("preview");
        preview1 = getIntent().getStringExtra("preview1");
        deviceViewModel = new ViewModelProvider(this, new DeviceViewModelFactory(new DeviceRepository())).get(DeviceViewModel.class);
        petViewModel = new ViewModelProvider(this, new PetViewModelFactory(new PetRepository())).get(PetViewModel.class);

        petViewModel.getFeedRuleList().observe(this, response -> {
            if(response == null || response.isEmpty()){
                mBinding.tvNextOutFeed.setText(getString(R.string.day_not_feed_rule));
            }else {
                boolean isHave = false;
                for (FeedRule feedRule : response) {
                    if (feedRule.getFeedStatus() == 0 && feedRule.getDateStatus() == 1) {
                        isHave = true;
                        mBinding.tvNextOutFeed.setText(String.format(getString(R.string.next_out_feed_time), feedRule.getStartTime()));
                        break;
                    }
                }
                if(!isHave){
                    mBinding.tvNextOutFeed.setText(getString(R.string.day_not_feed_rule));
                }
            }
        });

        petViewModel.getFeedRuleErrorList().observe(this, s -> {
            mBinding.tvNextOutFeed.setText(getString(R.string.day_not_feed_rule));
        });
        datesAndWeeks = DateUtils.getTodayAndNextDates(6);
        petViewModel.getFeedRuleList(deviceId, Constants.WEEKDAYSINT[datesAndWeeks.get(0).getWeek()], DateUtils.dateFormatDay1(datesAndWeeks.get(0).getDay()));

        feedLiveData();

        if (PermissionManager.checkPermissions(this)) {
            tokenLiveData();
        } else {
            ActivityCompat.requestPermissions(this, PermissionManager.getRequiredPermissions(), PERMISSION_REQ_ID);
        }

        feedingRecordsLiveData();
        onlineStatusLiveData();
        deviceViewModel.getFeedingRecordList(deviceId, date);
        GlideUtils.loadRoundedImage(this, preview, mBinding.ivPhoto);
        GlideUtils.loadRoundedImage(this, preview1, mBinding.ivPhoto1);
    }

    private void feedingRecordsLiveData() {
        deviceViewModel.getFeedingRecordDeviceLiveData().observe(this, apiResponse -> {
            feedingRecords.clear();
            feedingRecords.addAll(apiResponse.getData());
            mAdapter.notifyDataSetChanged();
            if (feedingRecords.isEmpty()) {
                mBinding.rvFeedingRecords.setVisibility(View.GONE);
                mBinding.llNotData.setVisibility(View.VISIBLE);
            } else {
                mBinding.rvFeedingRecords.setVisibility(View.VISIBLE);
                mBinding.llNotData.setVisibility(View.GONE);
            }
            mBinding.smartRefreshLayout.finishRefresh();
        });

        deviceViewModel.getFeedingRecordDeviceErrorLiveData().observe(this, s -> {
//            Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
            mBinding.smartRefreshLayout.finishRefresh();
        });
    }

    private void tokenLiveData() {
        deviceViewModel.getDeviceLiveData().observe(this, apiResponse -> {
            dismiss();
            token = apiResponse.getData().getToken();
            initRtc(0);
        });

        deviceViewModel.getDeviceLiveData1().observe(this, apiResponse -> {
            dismiss();
            token1 = apiResponse.getData().getToken();
            initRtc(1);
        });
        deviceViewModel.getErrorLiveData().observe(this, s -> {
            dismiss();
//            Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
        });
    }

    private void feedLiveData() {
        deviceViewModel.getFeedPlanDeviceLiveData().observe(this, apiResponse -> {
            dismiss();
            deviceViewModel.controlOpenFeed(deviceId, typePosition == 0 ? 1 : 0);
        });

        deviceViewModel.getFeedPlanErrorLiveData().observe(this, s -> {
            dismiss();
//            Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
        });


        deviceViewModel.getFeedDeviceLiveData().observe(this, apiResponse -> {
            dismiss();
            Toast.makeText(this, R.string.feed_success, Toast.LENGTH_SHORT).show();
        });

        deviceViewModel.getFeedErrorLiveData().observe(this, s -> {
            dismiss();
//            Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
        });
    }

    private void onlineStatusLiveData() {
        deviceViewModel.getDeviceStatusLiveData().observe(this, apiResponse -> {
            try {
                List<DeviceStatusResponse.DataDTO> data = apiResponse.getData();
                if (data != null && data.get(0) != null) {
                    device = data.get(0);

                    mBinding.tvToadyOutNumber.setText(device.getTodayToFoodNum() + "次");
                    mBinding.tvTodayOut.setText(device.getTodayToFoodG() + "g");
                    mBinding.tvTodayIn.setText(device.getTodayFromFoodG() + "g");

                    mBinding.tvCatStripsState.setText(device.getCatStripsState() == 1 ? getString(R.string.sufficient) : getString(R.string.impatient));
                    if (device.getCatStripsState() != 1) {
                        mBinding.tvCatStripsState.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_home_device_food_urgent, 0);
                    } else {
                        mBinding.tvCatStripsState.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                    }

                    mBinding.tvSolidFoodState.setText(device.getSolidFoodState() == 1 ? getString(R.string.sufficient) : getString(R.string.impatient));
                    if (device.getSolidFoodState() != 1) {
                        mBinding.tvSolidFoodState.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_home_device_food_urgent, 0);
                    } else {
                        mBinding.tvSolidFoodState.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                    }
                    mBinding.tvAuto.setText(device.getAutoFeedState() == 0 ? getString(R.string.not_open) : getString(R.string.open));
                    mBinding.tvGrainSurplus.setText(String.format(getString(R.string.grain_surplus),device.getWeight()));

                    String deviceId_4;
                    try {
                        deviceId_4 = deviceId.substring(deviceId.length() - 4);
                    } catch (Exception e) {
                        deviceId_4 = "";
                    }
                    mBinding.tvPetName.setText(device.getDeviceName() + "(" + deviceId_4 + ")");
                }

                if (data == null || data.get(0) == null || data.get(0).getOnlineStatus() != 1) {
                    MLog.i("设备不在线");
                    mBinding.onlineView.setBackground(AppCompatResources.getDrawable(this, R.drawable.layout_red_radius));
                    mBinding.tvDeviceOnline.setText(getString(R.string.device_offline));
                } else {
                    MLog.i("设备在线");
                    mBinding.onlineView.setBackground(AppCompatResources.getDrawable(this, R.drawable.layout_green_spot));
                    mBinding.tvDeviceOnline.setText(getString(R.string.device_online));
                }
                handler.sendEmptyMessageDelayed(GET_DEVICE_STATUS, 5000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            dismiss();
        });

        deviceViewModel.getDeviceStatusErrorLiveData().observe(this, s -> {
            dismiss();
            mBinding.onlineView.setBackground(AppCompatResources.getDrawable(this, R.drawable.layout_red_radius));
            mBinding.tvDeviceOnline.setText(getString(R.string.device_offline));
            handler.sendEmptyMessageDelayed(GET_DEVICE_STATUS, 5000);
        });
    }

    @Override
    public void onResume() {
        if (isBackPlayVideo) {
            isBackPlayVideo = false;
            // 如果已经授权，则初始化 RtcEngine 并加入频道
            if (PermissionManager.checkPermissions(this)) {
                deviceViewModel.getDeviceAccessToken(deviceId, userId);
                handler.postDelayed(()->{
                    deviceViewModel.getDeviceAccessToken1(deviceId+"_1", userId * 10000);
                },200);
            } else {
                ActivityCompat.requestPermissions(this, PermissionManager.getRequiredPermissions(), PERMISSION_REQ_ID);
            }
        }
        handler.sendEmptyMessage(GET_DEVICE_STATUS);

        super.onResume();
    }

    @Override
    public void onPause() {
        handler.removeCallbacksAndMessages(null);
        if (rtcEngineManager != null) {
            rtcEngineManager.closeVideo();
        }
        if (rtcEngineManager1 != null) {
            rtcEngineManager1.closeVideo();
        }
        super.onPause();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(LoginOut event) {
        handler.removeMessages(GET_DEVICE_STATUS);
    }

    @Override
    protected void initView() {
        mAdapter = new FeedingRecordsAdapter(this, feedingRecords);
        mBinding.rvFeedingRecords.setLayoutManager(new LinearLayoutManager(this));
        mBinding.rvFeedingRecords.setAdapter(mAdapter);


        mBinding.rbPet.setOnClickListener(onClickListener);
        mBinding.tvMore.setOnClickListener(onClickListener);
        mBinding.rbDevice.setOnClickListener(onClickListener);
        mBinding.ivBtn.setOnClickListener(onClickListener);
        mBinding.llFeed.setOnClickListener(onClickListener);
        mBinding.llLookTa.setOnClickListener(onClickListener);
        mBinding.ivCloseVideo.setOnClickListener(onClickListener);
        mBinding.ivVideoFullScreen.setOnClickListener(onClickListener);
        mBinding.ivFeedPlan.setOnClickListener(onClickListener);
        mBinding.ivBack.setOnClickListener(v -> {
            finish();
        });

        mBinding.remoteVideoViewContainer1.setOnClickListener(v -> {
            if(System.currentTimeMillis() - time > 1500){
                time = System.currentTimeMillis();
                switchLines();
            }
        });


        mBinding.smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            deviceViewModel.getFeedingRecordList(deviceId, date);
        });
    }
    private long time;
    public void switchLines() {
        ViewGroup parent = (ViewGroup) mBinding.remoteVideoViewContainer1.getParent();

        if (!(parent instanceof ConstraintLayout)) return;
        ConstraintLayout.LayoutParams paramsA = (ConstraintLayout.LayoutParams) mBinding.remoteVideoViewContainer.getLayoutParams();
        ConstraintLayout.LayoutParams paramsB = (ConstraintLayout.LayoutParams) mBinding.remoteVideoViewContainer1.getLayoutParams();

        ConstraintLayout.LayoutParams temp = new ConstraintLayout.LayoutParams(paramsA);
        mBinding.remoteVideoViewContainer.setLayoutParams(paramsB);
        mBinding.remoteVideoViewContainer1.setLayoutParams(temp);

        int indexA = parent.indexOfChild(mBinding.remoteVideoViewContainer);
        int indexB = parent.indexOfChild(mBinding.remoteVideoViewContainer1);

        parent.removeView(mBinding.remoteVideoViewContainer);
        parent.removeView(mBinding.remoteVideoViewContainer1);

        if (indexA < indexB) {
            parent.addView(mBinding.remoteVideoViewContainer1, indexA);
            parent.addView(mBinding.remoteVideoViewContainer, indexB);
        } else {
            parent.addView(mBinding.remoteVideoViewContainer, indexB);
            parent.addView(mBinding.remoteVideoViewContainer1, indexA);
        }

//        if (rtcEngineManager.getRemoteVideoViewContainer().getId() == mBinding.remoteVideoViewContainer.getId()) {
//            mBinding.remoteVideoViewContainer.removeAllViews();
//            mBinding.remoteVideoViewContainer1.removeAllViews();
//            rtcEngineManager1.checkVideo(mBinding.remoteVideoViewContainer);
//            handler.postDelayed(()->{
//                rtcEngineManager.checkVideo(mBinding.remoteVideoViewContainer1);
//            },200);
//        } else {
//            mBinding.remoteVideoViewContainer.removeAllViews();
//            mBinding.remoteVideoViewContainer1.removeAllViews();
//            rtcEngineManager.checkVideo(mBinding.remoteVideoViewContainer);
//            handler.postDelayed(()->{
//                rtcEngineManager1.checkVideo(mBinding.remoteVideoViewContainer1);
//            },200);
//        }
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == mBinding.ivCloseVideo.getId()) {
                rtcEngineManager.closeVideo();
                rtcEngineManager1.closeVideo();
                mBinding.llLookTa.setVisibility(View.VISIBLE);
                mBinding.ivPhoto.setVisibility(View.VISIBLE);
                mBinding.ivPhoto1.setVisibility(View.VISIBLE);
                mBinding.ivCloseVideo.setVisibility(View.INVISIBLE);
                mBinding.ivVideoFullScreen.setVisibility(View.INVISIBLE);
                mBinding.llRealSituation.setVisibility(View.INVISIBLE);
                mBinding.remoteVideoViewContainer.setVisibility(View.INVISIBLE);
                mBinding.remoteVideoViewContainer1.setVisibility(View.INVISIBLE);
            } else if (id == mBinding.llLookTa.getId()) {
                if (device.getOnlineStatus() == 1) {
                    if (!PermissionManager.checkStoragePermissions(DeviceInfoActivity.this)) {
                        requestPermissions(PermissionManager.getStoragePermissions(), 1);
                    }else {
                        if (PermissionManager.checkPermissions(DeviceInfoActivity.this)) {
                            handler.sendEmptyMessage(0);
                            mBinding.ivPhoto.setVisibility(View.INVISIBLE);
                            mBinding.ivPhoto1.setVisibility(View.INVISIBLE);
                            deviceViewModel.getDeviceAccessToken(deviceId, userId);
                            handler.postDelayed(() -> {
                                deviceViewModel.getDeviceAccessToken1(deviceId + "_1", userId * 10000);
                            }, 200);
                        } else {
                            ActivityCompat.requestPermissions(DeviceInfoActivity.this, PermissionManager.getRequiredPermissions(), PERMISSION_REQ_ID);
                        }
                    }
                }else {
                    Toast.makeText(DeviceInfoActivity.this,"设备不在线",Toast.LENGTH_SHORT).show();
                }

            } else if (id == mBinding.llFeed.getId()) {
                if (foodBottomSheetDialog == null) {
                    foodBottomSheetDialog = new FoodBottomSheetDialog(DeviceInfoActivity.this);
                    foodBottomSheetDialog.setConfirmButtonClickListener((typePosition, numberPosition) -> {
                        DeviceInfoActivity.this.typePosition = typePosition;
                        deviceViewModel.resetFeedNum(deviceId, typePosition == 0 ? numberPosition : 0, typePosition == 1 ? numberPosition : 0);
                    });
                }
                foodBottomSheetDialog.show();
            } else if (id == mBinding.ivBtn.getId()) {
                DeviceSettingsActivity.startActivity(DeviceInfoActivity.this, deviceId);
            } else if (id == mBinding.rbDevice.getId()) {
                mBinding.ivBack.setImageResource(R.drawable.ic_while_back);
                mBinding.ivBtn.setImageResource(R.drawable.ic_while_settings);
                getWindow().setStatusBarColor(Color.TRANSPARENT);
                mBinding.clTitle.setBackground(ResourcesCompat.getDrawable(getResources(), com.smart.base.R.color.transparent, null));
                mBinding.clVideo.setVisibility(View.GONE);
                mBinding.clPetDevice.setVisibility(View.VISIBLE);
            } else if (id == mBinding.rbPet.getId()) {
                mBinding.ivBack.setImageResource(R.drawable.icon_back);
                mBinding.ivBtn.setImageResource(R.drawable.ic_device_info_settings);
                getWindow().setStatusBarColor(Color.WHITE);
                mBinding.clTitle.setBackground(ResourcesCompat.getDrawable(getResources(), com.smart.base.R.color.white, null));
                mBinding.clVideo.setVisibility(View.VISIBLE);
                mBinding.clPetDevice.setVisibility(View.GONE);
            } else if (id == mBinding.ivVideoFullScreen.getId()) {
                isBackPlayVideo = true;
                rtcEngineManager.closeVideo();
                rtcEngineManager1.closeVideo();
                VideoCallActivity.startActivity(DeviceInfoActivity.this, deviceId);
            } else if (id == mBinding.ivFeedPlan.getId()) {
                FeedPlanActivity.startActivity(DeviceInfoActivity.this, deviceId);
            } else if (id == mBinding.tvMore.getId()) {
                FeedingRecordsActivity.startActivity(DeviceInfoActivity.this, deviceId, date);
            }
        }
    };

    @Override
    protected void onApplyWindowInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, 0, systemBars.right, systemBars.bottom);
            getWindow().setStatusBarColor(Color.WHITE);
            ViewGroup.MarginLayoutParams clTitleLayoutParams = (ViewGroup.MarginLayoutParams) mBinding.clTitle.getLayoutParams();
            clTitleLayoutParams.topMargin = systemBars.top;
            mBinding.clTitle.setLayoutParams(clTitleLayoutParams);
            return insets;
        });
    }

    @Override
    public void onPermissionsResultSuccess() {
        if (PermissionManager.checkPermissions(this)) {
            mBinding.ivPhoto.setVisibility(View.INVISIBLE);
            mBinding.ivPhoto1.setVisibility(View.INVISIBLE);
            handler.sendEmptyMessage(0);
            deviceViewModel.getDeviceAccessToken(deviceId, userId);
            handler.postDelayed(()->{
                deviceViewModel.getDeviceAccessToken1(deviceId+"_1", userId * 10000);
            },200);
        }
    }

    private void initRtc(int type) {
        if (type == 0) {
            rtcEngineManager = new RtcEngineManager(this, mBinding.remoteVideoViewContainer, token, deviceId, userId);
            rtcEngineManager.setRtcEngineEventListener(new RtcEngineManager.RtcEngineEventListener() {
                @Override
                public void onJoinChannelSuccess(String channel, int uid, int elapsed) {

                }

                @Override
                public void onUserOffline(int uid, int reason) {
                    mBinding.remoteVideoViewContainer.setVisibility(View.INVISIBLE);
                }

                @Override
                public void upLoadSuccess(String url) {
                    handler.sendEmptyMessageDelayed(4,5000);
                }
            });
            rtcEngineManager.joinChannel();
        } else {
            rtcEngineManager1 = new RtcEngineManager(this, mBinding.remoteVideoViewContainer1, token1, deviceId + "_1", userId * 10000);
            rtcEngineManager1.setRtcEngineEventListener(new RtcEngineManager.RtcEngineEventListener() {
                @Override
                public void onJoinChannelSuccess(String channel, int uid, int elapsed) {

                }

                @Override
                public void onUserOffline(int uid, int reason) {
                    mBinding.remoteVideoViewContainer1.setVisibility(View.INVISIBLE);
                }

                @Override
                public void upLoadSuccess(String url) {
                    handler.sendEmptyMessageDelayed(4,5000);
                }
            });
            rtcEngineManager1.joinChannel();
        }
    }


    @Override
    public void onPermissionsResultFail() {

    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            int what = msg.what;
            switch (what) {
                case 0: {
                    mBinding.llLookTa.setVisibility(View.INVISIBLE);
                    mBinding.ivCloseVideo.setVisibility(View.VISIBLE);
                    mBinding.ivVideoFullScreen.setVisibility(View.VISIBLE);
                    mBinding.llRealSituation.setVisibility(View.VISIBLE);
                    break;
                }
                case 2: {
                    mBinding.llLookTa.setVisibility(View.VISIBLE);
                    mBinding.ivCloseVideo.setVisibility(View.INVISIBLE);
                    mBinding.ivVideoFullScreen.setVisibility(View.INVISIBLE);
                    mBinding.llRealSituation.setVisibility(View.INVISIBLE);
                    break;
                }
                case GET_DEVICE_STATUS: {
                    deviceViewModel.getDeviceById(deviceId);
                    break;
                }
                case 4: {
                    MLog.e("=======接口上传=====");
                    if (rtcEngineManager != null && rtcEngineManager1 != null && (!TextUtils.isEmpty(rtcEngineManager.getAliImagePath()) || !TextUtils.isEmpty(rtcEngineManager.getAliImagePath()))) {
                        deviceViewModel.uploadDeviceVideoImg(deviceId, rtcEngineManager.getAliImagePath(), rtcEngineManager1.getAliImagePath());
                    }else {
                        handler.sendEmptyMessageDelayed(4,2000);
                    }
                    break;
                }
            }
            return false;
        }
    });

    public static void startActivity(Context context, String deviceId, String deviceName, String preview, String preview1) {
        Intent intent = new Intent(context, DeviceInfoActivity.class);
        intent.putExtra("deviceId", deviceId);
        intent.putExtra("deviceName", deviceName);
        intent.putExtra("preview", preview);
        intent.putExtra("preview1", preview1);
        context.startActivity(intent);
    }

    @Override
    public void networkStatus(boolean isConnected) {

    }

    @Override
    public void networkType(int type) {

    }
}